<?php
    class UsersModel
    {
        private $db;
        
        public function __construct() {
            $this->db = db::getInstance();
        }        
        
        // Get a single user
        public function getUser($id) {
            if(!empty($id)) {                
                $result = $this->db->select('*', 'users', array('where'=>array('id'=>$id)));
                return mysql_fetch_assoc($result);
            }
        }
        
        // Delete a user and the records where it holds a reference to
        public function deleteUser($id) {
            if(!empty($id)) {
                $this->db->delete('users', array('id'=>$id));
            }
        }
        
        // Add a user to the database
        public function addUser($values) {
            $this->db->insert('users', $values);
        }
        
        // Update a user in the database
        public function updateUser($id, $values) {
            $this->db->update('users', $values, array('id'=>$id));
        }
        
        /**
         * Get a list of all the records
         *
         * @return unknown	Array with the records
         */
        public function getRecords() {
            $records = array();
            $result = $this->db->select('*', 'users', array('orderby'=>'name'));
			while($record = mysql_fetch_assoc($result)) {
                array_push($records, $record);
            }
            return $records;
        }  
        
        public function checkLogin($username, $password) {
        	$numberOfRecords = $this->db->total('users', array('name'=>makeSafe($username), 'password'=>md5(makeSafe($password))));
        	if($numberOfRecords==1) {
        		return true;
        	} else {
        		return false;
        	}
        }
    }
?>